Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
JavaScript React setInterval "this" issue

I'm trying to learn React and I don't understand why this does not do anything when I try without an arrow function

componentDidMount() {
    var self = this
    this.timer = window.setInterval(function () {
        self.increment
    }, 1000)
}

Is the correct pattern self.increment()?

So why do I need the () when I don't need it in:

componentDidMount() {
    this.timer = window.setInterval(this.increment.bind(this),1000)
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are not invoking self.increment, you are properly scoping this for your code to work. Maybe it appears it's not working because your interval's callback does not do anything :)

Also, here's a simpler version of your code, you could omit the .bind if your function does not need to access your component's scope, but typically in react you'd want access to that:

this.timer = setInterval(this.increment.bind(this), 1000)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda